﻿using Microsoft.Extensions.DependencyInjection;
using Modules.Person.Repositories;
using Modules.System.Attachment;
using PmSoft;
using static Modules.Payroll.WageConstants;

namespace Modules.Payroll;

[UseDI(ServiceLifetime.Scoped)]
public class WageAttachmentService
{
    private readonly WageAttachmentRepository wageAttachmentRepository;
    private readonly AttachmentService attachmentService;

    public WageAttachmentService(WageAttachmentRepository wageAttachmentRepository, AttachmentService attachmentService)
    {
        this.wageAttachmentRepository = wageAttachmentRepository;
        this.attachmentService = attachmentService;
    }

    public void Create(WageAttachment entity)
    {
        if (string.IsNullOrEmpty(entity.AttachmentId))
            throw new ArgumentNullException(nameof(entity.AttachmentId));
        wageAttachmentRepository.Insert(entity);
        ToggleTemporaryAttachment(new List<WageAttachment> { entity });
    }

    public void Deleted(IEnumerable<string> ids)
    {
        foreach (string id in ids)
            DeleteAttachment(id);
    }

    /// <summary>
    /// 通过objectId获取附件集合
    /// </summary>
    /// <returns></returns>
    public IEnumerable<AttachmentInfo> GetSysAttachmentByObjectIdAndType(IEnumerable<object> objedctIds, 工资附件类型 attachmentType, 工资附件租户 tenant)
    {
        var data = wageAttachmentRepository.GetAttachmentsByObjectIds(objedctIds).Where(a => a.AttachmentType == attachmentType && a.Tenant == tenant);
        return attachmentService.GetAttachmentInfos(data.Select(a => a.AttachmentId));
    }

    /// <summary>
    /// 通过objectId获取附件集合
    /// </summary>
    /// <returns></returns>
    public IEnumerable<AttachmentInfo> GetSysAttachmentByObjectIdAndType(object objedctId, 工资附件类型 attachmentType, 工资附件租户 tenant)
    {
        var data = wageAttachmentRepository.GetAttachmentsByObjectId(objedctId).Where(a => a.AttachmentType == attachmentType && a.Tenant == tenant);
        return attachmentService.GetAttachmentInfos(data.Select(a => a.AttachmentId));
    }

    /// <summary>
    /// 通过objectId获取附件集合
    /// </summary>
    /// <returns></returns>
    public IEnumerable<WageAttachment> GetWageAttachmentByObjectIdAndType(object objedctId, 工资附件类型 attachmentType, 工资附件租户 tenant)
    {
        var data = wageAttachmentRepository.GetAttachmentsByObjectId(objedctId).Where(a => a.AttachmentType == attachmentType && a.Tenant == tenant);
        return data;
    }

    /// <summary>
    /// 通过objectId获取附件集合
    /// </summary>
    /// <returns></returns>
    public IEnumerable<WageAttachment> GetWageAttachmentByObjectIdsAndType(IEnumerable<object> objedctIds, 工资附件类型 attachmentType, 工资附件租户 tenant)
    {
        var data = wageAttachmentRepository.GetAttachmentsByObjectIds(objedctIds).Where(a => a.AttachmentType == attachmentType && a.Tenant == tenant);
        return data;
    }

    public void DeleteAttachment(string id)
    {
        wageAttachmentRepository.DeleteByEntityId(id);
    }

    /// <summary>
    /// 上传临时附件到MinIO
    /// </summary>
    /// <param name="notifyAttachments"></param>
    public void ToggleTemporaryAttachment(IEnumerable<WageAttachment> notifyAttachments)
    {
        attachmentService.ToggleTemporaryAttachments(notifyAttachments);
    }
}